﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using Showroom.BLL;

namespace ShowroomMobile
{
    public partial class UpdateSlideShow : AdminBasePage
    {
        private int SlideID
        {
            get
            {
                return string.IsNullOrEmpty(Request["id"]) ? 0 : Convert.ToInt32(Request["id"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (SlideID > 0)
                {
                    SlideShow slide = new SlideShow();
                    slide.LoadByPrimaryKey(SlideID);
                    if (slide.RowCount > 0)
                    {
                        txtTitle.Text = slide.s_Title;
                        txtDescription.Text = slide.s_Description;
                        txtURL.Text = slide.s_URL;
                        ckbActive.Checked = slide.Active;
                        string sImageUrl = "~/" + Utilities.GetKeySetting("PathUploadSlideShow") + "/" + slide.s_FileName;
                        imgFileName.ImageUrl = sImageUrl;
                    }
                    else
                    {
                        Response.Redirect("~/404.aspx");
                    }
                }
            }
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            try
            {
                SlideShow slide = new SlideShow();
                if (SlideID > 0)
                {
                    slide.LoadByPrimaryKey(SlideID);
                }
                else
                {
                    if (!fulImageFile.HasFile)
                    {
                        ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("alert('Please select an image file!');", 1), true);
                        return;
                    }
                    slide.AddNew();
                }
                if (fulImageFile.HasFile)
                {
                    string[] validExt = { "jpg", "png", "bmp", "gif" };
                    if (!validExt.Contains<string>(Path.GetExtension(fulImageFile.FileName).ToLower().Replace(".", "")))
                    {
                        ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("alert('Invalid image!');", 1), true);
                        return;
                    }
                    //Save Image Files
                    string filename = Guid.NewGuid().ToString() + Path.GetExtension(fulImageFile.FileName);
                    string path = Server.MapPath("~") + "\\" + Utilities.GetKeySetting("PathUploadSlideShow");
                    DirectoryInfo dInfo = new DirectoryInfo(path);
                    if (!dInfo.Exists)
                        dInfo.Create();
                    fulImageFile.SaveAs(Path.Combine(path, filename));
                    slide.FileName = filename;
                }
                slide.s_Title = txtTitle.Text.Trim();
                slide.s_Description = txtDescription.Text.Trim();
                slide.Active = ckbActive.Checked;
                slide.s_URL = txtURL.Text.Trim();
                slide.Save();
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("alert('Update successfully');", 1), true);
                if (SlideID == 0)
                    txtURL.Text = txtTitle.Text = txtDescription.Text = string.Empty;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("UpdateSlideShow.aspx.cs", "btnSave_OnClick", ex.StackTrace);
            }
        }
    }
}